package org.stragma.util

import scala.collection.mutable._

import org.kiama.rewriting._
import org.kiama.rewriting.Rewriter.{fail,id,rulefs,Strategy}

import org.stragma.apply.signatures.Options._
import org.stragma.apply.signatures._

/*
 * 	This object consists the Stratego functions used to store and retrieve configurations. A
 *	stored configuration is represented by an Options term which can be either "true" meaning
 *	"on" or "false" meaning "off"
 */
object Configuration {

	// This is a mutable map because configurations can be added to or removed from
	var configurations : Map[TBase,Boolean] = Map[TBase,Boolean]()

	/**
	 * 	If a configuration value exists, then return it as an
	 *	option(term) or return None
	 */
	def get_config = rulefs {
		case tbase : TBase if (configurations.contains(tbase)) =>
				configurations(tbase)
	}
	
	/**
	 * This helper method has been added for simpler inter-operability between
	 * Scala and Java
	 */
	def set_configHelper ( tbase : TBase, state : Boolean) = {
		set_config( ( tbase, state) )
	}
	
	/**
	 * Port of the Stratego set-config strategy
	 */
	def set_config = rulefs {
		case (tbase : TBase, state : Boolean) =>
		
			// If it is present update it's value only else add to the map
			if (configurations.contains(tbase))
				configurations(tbase) = state
			else
				configurations.put(tbase,state)
			
			// return the term passed in
			id
		case _ => fail
	}
	
	/**
	 * Port of the Stratego rm-config strategy, this removes a key from a Map
	 */
	def rm_config = rulefs {
		case tbase : TBase =>
			if (configurations.contains(tbase))
				configurations remove tbase
	}
}